package network.editor;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Action;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.TransferHandler;

import network.action.EditorActions;

import org.apache.log4j.Logger;

import xujun.control.XContorlUtil;

import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.swing.util.mxGraphActions;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxPoint;

/**
 * 
 * @author zhangzuoqiang <br/>
 *         Date: Aug 25, 2011
 */
public class EditorMenuBar extends JMenuBar {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unused")
	private static final Logger log = Logger.getLogger(EditorMenuBar.class);
	private BasicNetworkEditor networkEditor;

	public EditorMenuBar(BasicNetworkEditor editor) {
		networkEditor = editor;
		final mxGraphComponent graphComponent = editor.getGraphComponent();
		graphComponent.getGraph();
		JMenu menu = null;
		// 文件
		menu = add(new JMenu("文件"));
		menu.add(bind("新建", "images/new.png", new EditorActions.NewAction()));
		menu.add(bind("打开", "images/open.png", new EditorActions.OpenAction()));
		menu.addSeparator();
		menu.add(bind("保存", "images/save.png", new EditorActions.SaveAction(
				false)));
		menu.add(bind("另存为", null, new EditorActions.SaveAction(true)));
		menu.addSeparator();
		menu.add(bind("页面设置", null, new EditorActions.PageSetupAction()));
		menu.add(bind("打印", "images/print.png", new EditorActions.PrintAction()));
		menu.addSeparator();
		menu.add(bind("退出", null, new EditorActions.ExitAction()));
		// 编辑
		menu = add(new JMenu("编辑"));
		menu.add(bind("撤销", "images/undo.png", new EditorActions.HistoryAction(
				true)));
		menu.add(bind("恢复", "images/redo.png", new EditorActions.HistoryAction(
				false)));
		menu.addSeparator();
		menu.add(bind("剪切", "images/cut.png", TransferHandler.getCutAction()));
		menu.add(bind("复制", "images/copy.png", TransferHandler.getCopyAction()));
		menu.add(bind("粘贴", "images/paste.png",
				TransferHandler.getPasteAction()));
		menu.add(bind("删除", "images/delete.png",
				mxGraphActions.getDeleteAction()));
		menu.addSeparator();
		menu.add(bind("全部选择", null, mxGraphActions.getSelectAllAction()));
		menu.add(bind("取消选择", null, mxGraphActions.getSelectNoneAction()));
		// 视图
		menu = add(new JMenu("视图"));
		JMenuItem item = menu.add(new EditorActions.TogglePropertyItem(
				graphComponent, "页面模式", "PageVisible", true,
				new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						if (graphComponent.isPageVisible()
								&& graphComponent.isCenterPage()) {
							graphComponent.zoomAndCenter();
						} else {
							graphComponent.getGraphControl()
									.updatePreferredSize();
						}
					}
				}));
		item.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() instanceof EditorActions.TogglePropertyItem) {
					final mxGraphComponent graphComponent = networkEditor
							.getGraphComponent();
					EditorActions.TogglePropertyItem toggleItem = (EditorActions.TogglePropertyItem) e
							.getSource();

					if (toggleItem.isSelected()) {
						// 滚动视图到画布的中心位置
						SwingUtilities.invokeLater(new Runnable() {
							public void run() {
								graphComponent.scrollToCenter(true);
								graphComponent.scrollToCenter(false);
							}
						});
					} else {
						mxPoint tr = graphComponent.getGraph().getView()
								.getTranslate();
						if (tr.getX() != 0 || tr.getY() != 0) {
							graphComponent.getGraph().getView()
									.setTranslate(new mxPoint());
						}
					}
				}
			}
		});
		menu.addSeparator();
		menu.add(bind("放大", "images/zoomin.png",
				mxGraphActions.getZoomInAction()));
		menu.add(bind("缩小", "images/zoomout.png",
				mxGraphActions.getZoomOutAction()));
		menu.add(bind("还原", "images/zoomreset.png",
				mxGraphActions.getZoomActualAction()));
		menu.addSeparator();
		menu.add(new EditorActions.ToggleGridItem(editor, "网格画布"));
		menu.add(bind("虚连接线", null, new EditorActions.ToggleAction(
				mxConstants.STYLE_DASHED)));
		menu.add(bind("样式", null, new EditorActions.StyleAction()));
		
		// 帮助
		menu = add(new JMenu("帮助"));
		menu.add(bind("欢迎", null, new EditorActions.WelcomeAction()));		
	}

	// 创建一个和EditorAction绑定的按钮
	public JMenuItem bind(String text, String iconUrl, final Action action) {
		JMenuItem menuItem = new JMenuItem(text,
				iconUrl != null ? XContorlUtil.getImageIcon(iconUrl) : null);
		// 按钮事件触发绑定的EditorAction事件
		menuItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				action.actionPerformed(new ActionEvent(networkEditor
						.getGraphComponent(), e.getID(), e.getActionCommand()));
			}
		});
		return menuItem;
	}
}